<?php
namespace app\wumei\controller;

use app\admin\model\RouteModel;
use cmf\controller\HomeBaseController;
use app\wumei\model\WumeiCategoryModel;
use think\Db;
/**
 * Class WmCategoryApi
 * @package app\wumei\api
 * 物媒分类接口对接
 */
class ApiindexController extends HomeBaseController
{
    static $param;  //请求的参数
    //获取传过来的数据
    public function _initialize()
    {
            $data = $this->request->param();
            if(isset($data)&&$data!=[]){
                $data =  htmlspecialchars_decode($data['data']);
                self::$param  = json_decode($data,true);
            }
    }
    /**
     * 获取手机端展示的物媒分类
     */

    public function getWmCategory()
    {
        $categories = Db::name('portal_category')->order("list_order ASC")->where(['delete_time'=>0,'wmshow'=>2])->select()->toArray();
        return json($categories);
    }
    /**
     * 获取一条新闻的内容
     *
     */
    public function getOneArticle()
    {
        if(isset(self::$param['id']) && self::$param['id']>0){
            $data =   Db::name('portal_post');
            if(isset(self::$param['field'])){
               $data =  $data->field(self::$param['field']);
            }
            $data =  $data->where('id',self::$param['id'])->find();
          return json($data);
        }else{
            return false;
        }
    }
    /**
     * 获取一条信息
     *
     */
    public function getone($info=[])
    {
        $info = self::$param;
        $table = isset($info['table'])?$info['table']:'';
        if($table==''){
            return false;
        }
        $field = isset($info['field'])?$info['field']:'';
        $where = isset($info['where'])?$info['where']:'';
        $data = Db::name($table)->field($field)->where($where)->find();
        if ($data) {
            //如果是文章表
            if($table=='portal_post'){
                Db::name($table)->where($where)->setInc('post_hits');//自增
            }



            return json($data);
        } else {
            return null;
        }
    }
    /**
     * 获取很多数据
     * table  表名
     * where  条件
     * field   返回的字段
     * limit   分页
     * order   排序
     */
    public function getall()
    {
        $data = self::$param;
        if (!$data['table']) {
            return false;
        }
        $m = Db::name($data['table']);
        if (isset($data['field'])) {
            $m->field($data['field']);
        }
        if (isset($data['where'])) {
            $m->where($data['where']);
        }
        if(isset($data['whereIn'])){
            $m->whereIn($data['whereIn'][0],$data['whereIn'][1]);
        }
        if (isset($data['order'])) {
            $m->order($data['order']);
        }
        if (isset($data['limit'])) {
            $m->limit($data['limit']);
        }

        $list = $m->select()->toArray();



        if(count($list)==0){
            $list = null;
        }

//    $m->select();
//   $a  = $m->getLastSql();
//        return json($a);
//   die;
        return json($list);
    }

    /**
     *
     *
     * 根据分类id 获取文章标题列表
     *
     *
     * */
    public function UsePidGetTitle()
    {
        $category_id = self::$param['category_id']; //获取分类id
        $p = isset(self::$param['p'])?self::$param['p']:1;
        $num = isset(self::$param['num'])?self::$param['num']:10;
        $where = [];
        if(isset(self::$param['where'])){
            $where = self::$param['where'];
        }

       $list =   Db::name('portal_category_post')->alias('c')
            ->join('lzk_portal_post p','c.post_id=p.id')
            ->field('p.id,p.post_title,p.thumbnail,p.parent_id,p.video_url,p.post_source,p.post_hits')
            ->where(['c.status'=>1,'c.category_id'=>$category_id,'p.post_status'=>1])
            ->where($where)
           ->order('id desc')
            ->page($p,$num)
            ->select()
            ->toArray();

        if(count($list)==0){
            $list = null;
        }


       return json($list);
    }

    //获取物媒扫码页展示的文章分类，专门是扫码页的
    public function smindexCategory()
    {
        //这个分类只能是三级或二级分类

        $sql = 'select `id`,`name`,`path` from lzk_portal_category where parent_id in(select id from lzk_portal_category where issmindex ="2" and status="1" and parent_id=0) and status="1"';
      $list =   Db::query($sql);

      return json($list);

    }


}